package collection.list;

import java.util.ArrayList;

public class ArrayListSample {
    public static void main(String[] args) {
        ArrayList<String> bookList = new ArrayList<String>();
        bookList.add("鸡你太美");
        bookList.add("只因你太美");
        bookList.add("是否有根");
        // bookList.add(0);

        System.out.println(bookList);
        // Output: [鸡你太美, 只因你太美, 是否有根]

        // ----------- get -----------
        String bookName = bookList.get(2);
        System.out.println(bookName); // Output: 是否有根

        bookList.add(1, "小黑子");
        System.out.println(bookList);

        boolean result = bookList.add("苏珊");
        System.out.println(bookList);
        // Output: [鸡你太美, 小黑子, 只因你太美, 是否有根, 苏珊]
        System.out.println("列表是否发生变化：" + result);
        // Output: 列表是否发生变化：true

        // ----------- set -----------
        String before = bookList.set(3, "彻底没根");
        System.out.println(before);
        // Output: 是否有根
        System.out.println(bookList);
        // Output: [鸡你太美, 小黑子, 只因你太美, 彻底没根, 苏珊]


        // ----------- remove -----------
        boolean result2 = bookList.remove("小黑子");
        System.out.println(result2);
        // Output: true
        System.out.println(bookList);
        // Output: [鸡你太美, 只因你太美, 彻底没根, 苏珊]

        String item = bookList.remove(0);
        System.out.println(item);
        // Output: 鸡你太美
        System.out.println(bookList);
        // [只因你太美, 彻底没根, 苏珊]

        // ----------- size -----------
        int count = bookList.size();
        System.out.println(count);
        // Output: 3

        bookList.set(bookList.size() - 1, "荔枝");
        System.out.println(bookList);
        // Output: [只因你太美, 彻底没根, 荔枝]

        bookList.remove(bookList.size() - 1);
        System.out.println(bookList);
        // Output: [只因你太美, 彻底没根]


    }
}
